<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: '用户名' + '/' + '昵称' })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table />

        <!-- 表单 -->
        <PopupForm ref="popupForm" />
    </div>
</template>

<script setup lang="ts">
import { provide, useTemplateRef } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import { url, selTransferStatus, revokeTransfer } from '/@/api/backend/user/withdraw'

defineOptions({
    name: 'user/withdraw',
})

const { t } = useI18n()
const popupForm = useTemplateRef('popupForm')

// 支付宝 == 转账状态按钮 微信 == 撤销转账按钮
const payButton: OptButton[] = [
    {
        // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        render: 'tipButton',
        // 按钮名称，将作为触发表格内事件（onTableAction）时的事件名
        name: 'transferStatus',
        // 鼠标 hover 时的提示，可使用多语言翻译 key，比如 user.group
        title: '撤销转账',
        // 直接在按钮内显示的文字，title 有值时可为空，可使用多语言翻译 key，比如 user.group
        text: '撤销转账',
        // 按钮类型，请参考 element plus 的按钮类型
        type: 'primary',
        // 按钮 icon
        icon: 'fa fa-search-plus',
        class: 'table-row-info',
        click: (row: TableRow) => {
            revokeTransfer(row.id)
        },
        // 按钮是否显示，请返回布尔值
        display: (row: TableRow) => {
            return row.status == 'confirmed'
        },
        // 自定义其他 el-button 属性
        attr: {},
        // 是否禁用 title 提示，此值通常由系统动态调整以确保提示的显示效果
        disabledTip: true,
    },
    {
        // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        render: 'tipButton',
        // 按钮名称，将作为触发表格内事件（onTableAction）时的事件名
        name: 'transferStatus',
        // 鼠标 hover 时的提示，可使用多语言翻译 key，比如 user.group
        title: '转账状态',
        // 直接在按钮内显示的文字，title 有值时可为空，可使用多语言翻译 key，比如 user.group
        text: '转账状态',
        // 按钮类型，请参考 element plus 的按钮类型
        type: 'primary',
        // 按钮 icon
        icon: 'fa fa-search-plus',
        class: 'table-row-info',
        click: (row: TableRow) => {
            selTransferStatus(row.id)
        },
        // 按钮是否显示，请返回布尔值
        display: (row: TableRow) => {
            return row.status == 'successed'
        },
        // 自定义其他 el-button 属性
        attr: {},
        // 是否禁用 title 提示，此值通常由系统动态调整以确保提示的显示效果
        disabledTip: true,
    },
]

let optButtons = defaultOptButtons(['edit', 'delete'])
optButtons = payButton.concat(optButtons)

optButtons[3].display = (row) => {
    return row.status == 'created'
}

const baTable = new baTableClass(
    new baTableApi(url),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70 },
            { label: '用户ID', prop: 'user_id', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: '昵称', prop: 'user.nickname', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: '金额', prop: 'money', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            {
                label: '手续费',
                prop: 'handingfee',
                align: 'center',
                operator: 'LIKE',
            },
            { label: '税费', prop: 'taxes', align: 'center' },
            {
                label: '类型',
                prop: 'type_text',
                align: 'center',
                render: 'tag',
                replaceValue: { alipay: '支付宝', wechat: '微信' },
            },
            { label: '提现账户', prop: 'account', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            {
                label: '真实姓名',
                prop: 'name',
                align: 'center',
                operator: 'LIKE',
            },
            { label: '订单号', prop: 'orderid', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: '流水号', prop: 'transactionid', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: '备注', prop: 'memo', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: { created: 'primary', successed: 'success', rejected: 'danger', confirmed: 'warning' },
                replaceValue: { created: '申请中', successed: '成功', rejected: '拒绝', confirmed: '待确认' },
            },
            { label: t('Create time'), prop: 'createtime', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            { label: '转账时间', prop: 'transfertime', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            {
                label: t('Operate'),
                align: 'center',
                width: '150',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            status: 'created',
        },
    },
    {},
    {
        getEditData({ res }) {
            if (res && res.data) {
                baTable.form.items = res.data
                popupForm.value!.status = res.data.status
            }
        },
    }
)

baTable.mount()
baTable.getData()

provide('baTable', baTable)
</script>

<style scoped lang="scss"></style>
